package org.aplikator.client.shared.descriptor;

import java.util.ArrayList;
import java.util.List;

import org.jboss.errai.common.client.api.annotations.Portable;

@SuppressWarnings("serial")
@Portable
public class ViewDTO extends ClientDescriptorBase implements Cloneable {

    
    private List<QueryDescriptorDTO> filters = new ArrayList<QueryDescriptorDTO>();

    private List<SortDescriptorDTO> sorts = new ArrayList<SortDescriptorDTO>();

    private List<FunctionDTO> functions = new ArrayList<FunctionDTO>();

    private String activeFilter = "";

    private String activeSort = "";

    private String searchString = null;

    private FormDTO formDescriptor;

    private EntityDTO entity;

    private int pageSize;


    @SuppressWarnings("unused")
    public ViewDTO() {          //TODO custom marshaller, make private
    }

    public ViewDTO(String id, String localizedName, int pageSize) {
        super(id, localizedName);
        this.pageSize = pageSize;
    }


    public FormDTO getFormDescriptor() {
        return formDescriptor;
    }

    public void setFormDescriptor(FormDTO formDescriptor) {
        this.formDescriptor = formDescriptor;
    }

    public int getPageSize() {
        return pageSize;
    }
    
    public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

    public EntityDTO getEntity() {
        return entity;
    }

    public void setEntity(EntityDTO entity) {
        this.entity = entity;
    }

    public String getSearchString() {
        return searchString;
    }

    public void setSearchString(String searchString) {
        this.searchString = searchString;
    }

    public ViewDTO clone() {
        ViewDTO retval = new ViewDTO(this.getId(), this.getLocalizedName(), this.getPageSize());
        retval.setLocalizedName(this.getLocalizedName());
        retval.formDescriptor = this.formDescriptor;
        retval.entity = this.entity;
        for (QueryDescriptorDTO qd: filters){
            retval.addQueryDescriptor(qd.clone());
        }
        retval.sorts = this.sorts;
        retval.functions = this.functions;
        retval.searchString = this.searchString;
        retval.activeSort = this.activeSort;
        retval.activeFilter = this.activeFilter;
        return retval;
    }


    public ViewDTO addQueryDescriptor(QueryDescriptorDTO queryDescriptorDTO){
        filters.add(queryDescriptorDTO);
        return this;
    }

    public List<QueryDescriptorDTO> getQueryDescriptors(){
        return filters;
    }
    
    public void setQueryDescriptors(List<QueryDescriptorDTO> descs) {
    	this.filters = descs;
    }
    

    public List<QueryParameter> getActiveQueryParameters(){
        if (activeFilter!= null && !activeFilter.equals("")){
            for (QueryDescriptorDTO qd: filters){
                if (qd.getId().equals(activeFilter)){
                    return qd.getQueryParameters();
                }
            }
            throw new IllegalStateException("QueryDescriptor "+activeFilter+" not found");
        } else if (filters.size()>0) {
            activeFilter = filters.get(0).getId();
            return filters.get(0).getQueryParameters();
        }
        return null;
    }


    public ViewDTO addSortDescriptor(SortDescriptorDTO sortDescriptorDTO){
        sorts.add(sortDescriptorDTO);
        return this;
    }

    public List<SortDescriptorDTO> getSortDescriptors(){
        return sorts;
    }
    
    public void setSortDescriptors(List<SortDescriptorDTO> sorts) {
    	this.sorts = sorts;
    }

    //@JsonIgnore
    public PropertyDTO getActivePrimarySortProperty(){
        if (activeSort!= null && !activeSort.equals("")){
            for(SortDescriptorDTO sd: sorts){
                if(sd.getId().equals(activeSort)){
                    return sd.getPrimarySortProperty();
                }
            }
            throw new IllegalStateException("SortDescriptor "+activeSort+" not found");
        } else if (sorts.size()>0){
            activeSort = sorts.get(0).getId();
            return sorts.get(0).getPrimarySortProperty();
        }
        return null;
    }



    public ViewDTO addFunction(FunctionDTO functionDTO){
        functions.add(functionDTO);
        return this;
    }

    public List<FunctionDTO> getFunctions(){
        return functions;
    }

    public String getActiveFilter() {
        return activeFilter;
    }

    public void setActiveFilter(String activeFilter) {
        this.activeFilter = activeFilter;
    }

    public String getActiveSort() {
        return activeSort;
    }

    public void setActiveSort(String activeSort) {
        this.activeSort = activeSort;
    }


}
